/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/* Forum.java
 *
 * Created on 08/04/2011, 16:53:46
 */
package GUI;

import Business.FrameHandler;
import Domain.DataManager;
import Domain.UserClient.ButtonsFunctions;
import Logger.ClientLogger;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import objects.*;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;

/**
 *
 * @author Inbal Cohen
 */
public class Forum extends javax.swing.JFrame {

    private List<Trd> _threads;
    private Trd _trd = null;
    private Message _msg;
    List <Message> _messages;

    /** Creates new form Forum */
    public Forum(List<Trd> threads) {
        this._threads = threads;
        initComponents();
        jLabel1.setVisible(false);
        Delete.setVisible(false);
        Edit.setVisible(false);
        forumHeadline.setText("Forum "+ButtonsFunctions.getButtonsFunctions().getCurrentForum());

        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        setLocation(screenWidth / 4, screenHeight / 4);
       
        DefaultListModel threadList = new DefaultListModel();

        for (Iterator<Trd> it = _threads.iterator(); it.hasNext(); )
        {
            Trd trd = it.next();  // No downcasting required.
            threadList.addElement(trd.getThreadName());
            this.ThreadList.setModel(threadList);
        }
         if(DataManager.getDataManager().getCurrTypeOfUser() == 0)
         {
                 AddThread.setVisible(false);
         }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        ThreadList = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        TitleMessages = new javax.swing.JList();
        forumHeadline = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        MessageText = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        Delete = new java.awt.Button();
        addPost = new java.awt.Button();
        Edit = new java.awt.Button();
        AddThread = new java.awt.Button();
        back = new javax.swing.JButton();
        refresh = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Showing Thread & Messages");
        setMinimumSize(new java.awt.Dimension(700, 400));

        ThreadList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                ThreadListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(ThreadList);

        TitleMessages.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                TitleMessagesValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(TitleMessages);

        forumHeadline.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        forumHeadline.setForeground(new java.awt.Color(153, 0, 255));
        forumHeadline.setText("Threads and Messages");

        MessageText.setColumns(20);
        MessageText.setRows(5);
        jScrollPane3.setViewportView(MessageText);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 102, 255));
        jLabel2.setText("Title Message");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(0, 102, 204));
        jLabel3.setText("Message Text");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(51, 102, 255));
        jLabel4.setText("Threads");

        Delete.setActionCommand("Delete");
        Delete.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        Delete.setLabel("Delete");
        Delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteActionPerformed(evt);
            }
        });

        addPost.setLabel("Add Post");
        addPost.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addPostActionPerformed(evt);
            }
        });

        Edit.setLabel("Edit");
        Edit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EditActionPerformed(evt);
            }
        });

        AddThread.setLabel("Add Thread");
        AddThread.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddThreadActionPerformed(evt);
            }
        });

        back.setText("Back");
        back.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backActionPerformed(evt);
            }
        });

        refresh.setText("Refresh");
        refresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 0, 51));
        jLabel1.setText("user and time will be shown here");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(refresh)
                        .addGap(128, 128, 128)
                        .addComponent(forumHeadline))
                    .addComponent(back)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel4)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(21, 21, 21)
                                        .addComponent(jLabel2))
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(22, 22, 22)
                                .addComponent(AddThread, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(99, 99, 99)
                                .addComponent(addPost, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(87, 87, 87)
                                .addComponent(jLabel3))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(42, 42, 42)
                                .addComponent(Delete, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 138, Short.MAX_VALUE)
                                .addComponent(Edit, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(34, 34, 34))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE))))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(back)
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(refresh, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(forumHeadline))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(AddThread, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(addPost, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel1)
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(Delete, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Edit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void DeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteActionPerformed
         ClientLogger.write(Level.INFO,"deleting Thread: "+ _trd.getThreadName() +",  Title: "+ _msg.getTitle());
        ButtonsFunctions.getButtonsFunctions().processRemoveMessage(_trd.getThreadName(),_msg.getTitle());
        if(DataManager.getDataManager().getSuccessOrFailure() == DataManager.FAILURE)
                return;
        
        String currForum = ButtonsFunctions.getButtonsFunctions().getCurrentForum();
        ButtonsFunctions.getButtonsFunctions().processGetThreads(currForum);
        String forum = DataManager.getDataManager().getCurrForum();
        Vector<Trd> threads = DataManager.getDataManager().getVectorThreads();
        FrameHandler.getFrameHandler().changeCurrentFrame(new Forum(threads));
    }//GEN-LAST:event_DeleteActionPerformed

    private void AddThreadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddThreadActionPerformed
       JFrame addThread = new AddThread();
       addThread.setVisible(true);
    }//GEN-LAST:event_AddThreadActionPerformed

    private void addPostActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPostActionPerformed
        if(_trd != null){
            JFrame newPost = new AddPost(_trd.getThreadName());
            newPost.setVisible(true);
        }
        else{
            String error = "You have to choose a thread subject before posting a message";
            FrameHandler.getFrameHandler().showErrorMessage(error);
        }
    }//GEN-LAST:event_addPostActionPerformed

    private void backActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backActionPerformed
       ButtonsFunctions.getButtonsFunctions().processGetForums();
       int type = DataManager.getDataManager().getCurrTypeOfUser();
       Vector<Frm> forums = DataManager.getDataManager().getVectorForums();
       FrameHandler.getFrameHandler().changeCurrentFrame(new ChooseForum(forums, type));
       DataManager.getDataManager().setCurrForum("");
    }//GEN-LAST:event_backActionPerformed

    private void ThreadListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_ThreadListValueChanged

        changeViewButtons(false, false);
        MessageText.setText("");
        
        if(!ThreadList.isSelectionEmpty())
        {
                _trd = _threads.get(ThreadList.getSelectedIndex());
                 ButtonsFunctions.getButtonsFunctions().processGetMessages(_trd.getThreadName());
                if(DataManager.getDataManager().getSuccessOrFailure() == DataManager.FAILURE)
                    return;
                _messages = DataManager.getDataManager().getMessages();
         //       _messages = _trd.getMessages();
         //      System.out.println( _messages);
                DefaultListModel messageList = new DefaultListModel();
                for (int i=0; i < _messages.size(); i++ )
                {
                    Message msg = _messages.get(i);  // No downcasting required.
                    messageList.addElement(msg.getTitle());
                }
                this.TitleMessages.setModel(messageList);
        }
    }//GEN-LAST:event_ThreadListValueChanged

    private void TitleMessagesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_TitleMessagesValueChanged

        MessageText.setText("");


        if(!TitleMessages.isSelectionEmpty())
        {
            if(_messages != null)
            {
                int index = TitleMessages.getSelectedIndex();
                this._msg = _messages.get(index);//.getMessage();
                if(DataManager.getDataManager().getCurrTypeOfUser() == 1 ||
                    _msg.getOwner().getUserName().equals(ButtonsFunctions.getButtonsFunctions().getCurrUser())){
                    changeViewButtons(true, true);
                }
                else{
                    changeViewButtons(false,false);
                }
                if(index<_messages.size())
                {
                    MessageText.setCaretColor(Color.red);
                    MessageText.setText(_msg.getMessage());
                  
                    jLabel1.setText("Published by "+ _msg.getOwner().getUserName() +" in "+ _msg.getDate());
                    jLabel1.setVisible(true);
                 
                }
            }
        }
    }//GEN-LAST:event_TitleMessagesValueChanged

    private void EditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EditActionPerformed
         if(_msg != null){
            JFrame newMes = new EditWindow(this._trd,_msg.getTitle(),_msg.getMessage());
            newMes.setVisible(true);
        }
        else{
            String error = "You have to choose a message to edit";
            FrameHandler.getFrameHandler().showErrorMessage(error);
        }
    }//GEN-LAST:event_EditActionPerformed

    private void refreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshActionPerformed
        this.dispose();
        String forum = ButtonsFunctions.getButtonsFunctions().getCurrentForum();
        ButtonsFunctions.getButtonsFunctions().processGetThreads(forum);
        // open new window here new ChooseMove(_control, this,pPilot,cPilot).setVisible(true);
        Vector<Trd> trds = DataManager.getDataManager().getVectorThreads();
        FrameHandler.getFrameHandler().changeCurrentFrame(new Forum(trds));
    }//GEN-LAST:event_refreshActionPerformed

      private void changeViewButtons(boolean delete, boolean edit)
      {
        Delete.setVisible(delete);
        Edit.setVisible(edit);
    }
  

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Button AddThread;
    private java.awt.Button Delete;
    private java.awt.Button Edit;
    private javax.swing.JTextArea MessageText;
    private javax.swing.JList ThreadList;
    private javax.swing.JList TitleMessages;
    private java.awt.Button addPost;
    private javax.swing.JButton back;
    private javax.swing.JLabel forumHeadline;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JButton refresh;
    // End of variables declaration//GEN-END:variables

}
